# ---------- ---------- ---------- ---------- ---------- ----------
#
# dotfiles
#
# ---------- ---------- ---------- ---------- ---------- ----------


NEW_HOME=$$(pwd)/var


target: help


# ---------- FUNCTIONS  ---------- ---------- ---------- ----------
define diff_file
	@if [ -d $(1) ]; then\
            echo "going into directory...";\
        elif [ ! -f $(1) ]; then\
            echo "cannot find src $(1)";\
        elif [ ! -f $(2) ]; then\
            echo "cannot find dest $(2)";\
        else\
            diff --color=always $(1) $(2) || echo "WARNING: dirty: $(1) $(2)";\
        fi
endef


define cp_file
	@if [ -d $(1) ]; then\
            rsync -avh --delete $(1) $(2);\
        elif [ ! -f $(1) ]; then\
            echo "cannot find src $(1)";\
        else\
            mkdir -p $$(dirname $(2));\
            cp $(1) $(2);\
        fi
endef


define do_func
	@$(call $(1),./src/bash/.bash_aliases,$(2)/.bash_aliases)
	@$(call $(1),./src/bash/.bashrc,$(2)/.bashrc)

	@$(call $(1),./src/emacs/.emacs,$(2)/.emacs)
	@$(call $(1),./src/emacs/.emacs.d/opt/my-theme-file.el,$(2)/.emacs.d/opt/my-theme-file.el)

	@$(call $(1),./src/git/.gitignore_global,$(2)/.gitignore_global)
	@$(call $(1),./src/git/.gitconfig,$(2)/.gitconfig)

	@$(call $(1),./src/mutt/muttrc,$(2)/.mutt/muttrc)
	@$(call $(1),./src/mutt/mailcap,$(2)/.mutt/mailcap)

	@$(call $(1),./src/starship/config.toml,$(2)/.starship/config.toml)

	@$(call $(1),./src/vim/.vimrc,$(2)/.vimrc)
endef


# ---------- PHONIES -- ---------- ---------- ---------- ----------
.PHONY: help
help:
	@echo "Usage: make [PHONY]"
	@sed -n -e "/sed/! s/\.PHONY: //p" Makefile


.PHONY: clean
clean:
	@rm -rf ./var


.PHONY: diffs
diffs:
	@$(call do_func,diff_file,~)


.PHONY: home
home:
	@mkdir -p $(NEW_HOME)
	@$(call do_func,cp_file,$(NEW_HOME))
	@echo "Now run:"
	@echo "    env -i \\"
	@echo "        HOME=$(NEW_HOME) \\"
	@echo "        DISPLAY=:0.0 \\"
	@echo "        COLORTERM=truecolor \\"
	@echo "        TERM=xterm-256color \\"
	@echo "        bash"


.PHONY: install
install:
	@$(call do_func,cp_file,~)


.PHONY: rehome
rehome: clean home
